from flask import Flask,render_template

import sqlite3
from collections import Counter


app = Flask(__name__)


# 控制全局的url域名
def natapp():
    natapp_url = "http://127.0.0.1:5000/"
    return natapp_url

url = natapp()


@app.route('/')
def hello_world():  # put application's code here
    return render_template('index.html',url=url)

# 首页
@app.route('/index')
def index():
    return render_template('index.html',url=url)

# 电影列表
@app.route('/movie_list')
def movie_list():
    conn = sqlite3.connect("Script/douban.db")
    select_sql = "select * from douban_movies"

    cur = conn.cursor()  # 建立游标对象
    cur.execute(select_sql)  # 查询数据
    conn.commit()  # 提交
    movies = cur.fetchall()
    # print(movies)
    cur.close()  # 关闭游标对象
    conn.close()  # 关闭数据库连接


    return render_template('movie_list.html',movies=movies,url=url)

# 图表
@app.route('/diagram')
def diagram():
    conn = sqlite3.connect("Script/douban.db")
    select_sql = "select movie_name,scores from douban_movies"

    cur = conn.cursor()  # 建立游标对象
    cur.execute(select_sql)  # 查询数据
    conn.commit()  # 提交
    movies = cur.fetchall()
    # print(movies)
    cur.close()  # 关闭游标对象
    conn.close()  # 关闭数据库连接
    movie_name = [i[0] for i in movies]
    # print(movie_name)

    # 遍历元组，取值列表数据
    scores = [i[1] for i in movies]
    # 去重
    set_list = list(set(scores))
    print(set_list)

    # 找出重复数据的次数 得出的是Counter类型的数据 将Counter类型转换成字典数据
    count = dict(Counter(scores))
    print(count)
    # 建立一个新列表，用于存放出现重复元素的次数，与set_list一一对应
    new_count_list = []
    for i in set_list:
        new_count_list.append(count[i])
    print(f"数据统计:{new_count_list}")



    return render_template('diagram.html',new_count_list=new_count_list,scores=set_list,url=url)

# 词云
@app.route('/wordCloud')
def wordCloud():
    return render_template('wordCloud.html',url=url)

# 团队
@app.route('/team')
def team():
    return render_template('team.html',url=url)

if __name__ == '__main__':
    app.run()